define(function(require, exports, module) {

	var $ = require('jquery');
	require('artTemplate/artTemplate/2.0.1/template.js');
	require('jquery/jquery-form/1.0.0/jquery.form.js');
	require('jquery/jquery-json/2.4.0/jquery.json.js');
	require('seajs/seajs/2.1.1/sea.js');
	require('jquery/jquery-validation/1.11.0/jquery.validate');
	require('jquery/jquery-validation/1.11.0/messages_zh');
	require('jbox');
	
	exports.options = {
		listLink : '',
		listParams : '',
		pageLink : '',
		pageParams : ''
	};
	
	if($("#uid").val()!=g_user.id){
		$(".other").attr({style:"display:block"});
	}else {
		$(".legend").html("修改当前管理员账号信息");
		$(".own").attr({style:"display:block"});
	}
	
	//选择角色类型列表
	exports.showRolesTypeList = function showRolesTypeList() {
		var getting = $.get($CONFIG.base_url + "/api/roles");
		getting.done(function(data) {
			$.each(data,function(key,val){
				var $option = $("<option id='" + val.id + "' value= '"+val.id+"'>"+ val.roleName + "</option>");
				$("#roles_select").append($option);
			});
		});
		
	};
	exports.showRolesTypeList();
	
	$("#update-form").validate({
		rules : {
			loginName:{
				required:true,
/*				email:true
			},
			email:{
				required:true,
				email:true
*/			},
			name:{
				required:true,
			},
			plainPassword:{
				required:true,
				rangelength:[6,18],
			}
		},
		messages : {
			loginName : {
				required:function(){
					$.jBox.tip("登录名不能为空!");
				},
/*				email:function(){
					$.jBox.tip("登录名必须为邮箱格式!");
				}
			},
			email : {
				required:function(){
					$.jBox.tip("邮箱不能为空!");
				},
				email:function(){
					$.jBox.tip("邮箱必须为邮箱格式!");
				}
*/			},
			name:{
				required:function(){
					$.jBox.tip("用户名不能为空!");
				},
			},
			plainPassword:{
				required:function(){
					$.jBox.tip("密码名不能为空!");
				},
				rangelength:function(){
					$.jBox.tip("密码为6-18位！");
				},
			}
		},
	});
	
	$("#control-pw").on("click",function(){
		if($(this).attr('data')=='1'){
			$(this).text('不修改密码');
			$(this).after('<div class="form-group" id="pw"><label for="plainPassword">密码</label><div class=""><input type="password" id="plainPassword" name="plainPassword" class="form-control"/></div></div>');
			$(this).attr({data:'0'});
		}else if($(this).attr('data')=='0'){			
			$(this).text('修改密码');
			$("#pw").remove();
			$(this).attr({data:'1'});
		}
	});
	
	$("#control-email").on("click",function(){
		if($(this).attr('data')=='1'){
			$(this).text('不修改邮箱');
			$("#show-email").show();
			$(this).attr({data:'0'});
		}else if($(this).attr('data')=='0'){			
			$(this).text('修改邮箱');
			$("#show-email").hide();
			$(this).attr({data:'1'});
		}
	});
	
	$('.add-submit').click(function(){
		if(!$("#update-form").valid()){
			return;
		}
		var postData = form2js('update-form', '.', true);
		postData["roles"]={"id":postData["rolesId"]};
		delete postData["rolesId"];
		//console.log(postData);
		$.ajax({
			url : $CONFIG.base_url + "/api/admin/user/update",
			type : "POST",
			data : $.toJSON(postData),
			dataType : "json",
			contentType : "application/json",				
			success : function() {
				$.jBox.tip("更新用户信息成功！");
				setTimeout(function(){window.location.href=$CONFIG.base_url + '/'+$CONFIG.admin_url+'/user/list';},1000);
			},
			error : function() {
				$.jBox.tip("更新用户信息失败！");
			}
		});
	});
});